package com.cta.dao.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cta.constant.AreaCodeType;
import com.cta.dao.TariffRecordPersistence;
import com.cta.entity.TariffAreaCode;
import com.cta.entity.TariffGroup;
import com.cta.entity.TariffRecord;
import com.cta.utils.SupportLoaderDelegate;

@Repository("tariffRecordPersistence")
public class TariffRecordPersistenceImpl extends BasePersistenceImpl implements TariffRecordPersistence {

	public TariffRecord getTariffRecord(Long tariffId) {
		return findById(TariffRecord.class, tariffId);
	}

	public TariffRecord getTariffRecord(Long tariffId, SupportLoaderDelegate<TariffRecord> loader) {
		return findById(TariffRecord.class, tariffId, loader);
	}

	public TariffRecord getTariffRecordWidthDiscount(Long tariffId) {
		return findSingleObject("select t from TariffRecord t left join fetch t.discountRates where t.id = ?1",
				tariffId);
	}

	public TariffGroup getTariffGroup(Long tariffGroupId, SupportLoaderDelegate<TariffGroup> loader) {
		return findById(TariffGroup.class, tariffGroupId, loader);
	}

	public TariffAreaCode getTariffAreaCode(Long tariffAreaCodeId, SupportLoaderDelegate<TariffAreaCode> loader) {
		return findById(TariffAreaCode.class, tariffAreaCodeId, loader);
	}

	public List<TariffRecord> listTariffRecord() {
		return getList("select t from TariffRecord t order by t.id");
	}
	
	public List<TariffRecord> listAllTariffRecordWidthDiscount() {
		return getList("select t from TariffRecord t left join fetch t.discountRates");
	}

	public List<TariffGroup> listAllTariffGroup(Long tariffId) {
		return getList("select t from TariffGroup t where t.tariffRecord.id = ?1 order by t.id", tariffId);
	}

	public List<TariffGroup> listAllTariffGroupWithDiscount(Long tariffId) {
		return getList(
				"select t from TariffGroup t left join fetch t.discountRates where t.tariffRecord.id = ?1 order by t.id",
				tariffId);
	}

	public List<TariffAreaCode> listAllTariffGroup(List<Long> ids) {
		StringBuffer sql = new StringBuffer("select t from TariffAreaCode t where t.tariffGroup.id in (");
		for (int i = 1; i <= ids.size(); i++) {
			if (i == ids.size()) {
				sql.append("?" + i + ")");
			} else {
				sql.append("?" + i + ", ");
			}
		}
		sql.append(" order by t.type desc, t.id");
		return getListWithArray(sql.toString(), ids.toArray());
	}

	@Override
	public List<TariffAreaCode> listAllTariffAreaCodeWithRate(Collection<Long> ids) {
		StringBuffer sql = new StringBuffer(
				"select t from TariffAreaCode t left join fetch t.areaCodeRates where t.tariffGroup.id in (");
		for (int i = 1; i <= ids.size(); i++) {
			if (i == ids.size()) {
				sql.append("?" + i + ")");
			} else {
				sql.append("?" + i + ", ");
			}
		}
		sql.append(" order by t.type desc, t.id");
		return getListWithArray(sql.toString(), ids.toArray());
	}

	public List<TariffAreaCode> listTariffAreaCodeTemplate(Long groupId) {
		return getList("select t from TariffAreaCode t where t.tariffGroup.id = ?1 and t.type = ?2 order by t.id",
				groupId, AreaCodeType.TEMPLATE);
	}

}
